// 请求地址
 let baseUrl = "http://api.qipadu.com/api" //'http://api.qipadu.com/api'
export default {
  get(url, param) {
    return new Promise((resolve, reject) => {
      uni.getStorage({
        key: 'isLogin',
        success: ress=> {
          let token = ress.data
          uni.request({
            method: 'GET',
            url: baseUrl + url,
            data: param,
            header: {
              'Authorization': token,
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
						checkResponseCode(res[1].data)
          }).catch(err => {
            resolve(err)
          })
        },
        fail: function(err) {
          uni.request({
            method: 'GET',
            url: baseUrl + url,
            data: param,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
            checkResponseCode(res[1].data)
          }).catch(err => {
            resolve(err)
          })
        }
      });
    })
  },
  post(url, data) {
    return new Promise((resolve, reject) => {
      uni.getStorage({
        key: 'isLogin',
        success: ress=> {
          let token = ress.data
          uni.request({
            method: 'POST',
            url: baseUrl + url,
            data: data,
            header: {
              'Authorization': token,
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
						console.log(res)
            resolve(res[1].data)
            checkResponseCode(res[1].data)
          }).catch(err => {
            console.log(err)
            resolve(err)
          })
        },
        fail: function(err) {
          uni.request({
            method: 'POST',
            url: baseUrl + url,
            data: data,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
            checkResponseCode(res[1].data)
          }).catch(err => {
            resolve(err)
						console.log(111)
          })
        }
      });

    })
  },
  put(url, data) {
    return new Promise((resolve, reject) => {
      uni.getStorage({
        key: 'isLogin',
        success: ress=> {
          let token = ress.data
          uni.request({
            method: 'PUT',
            url: baseUrl + url,
            data: data,
            header: {
              'Authorization': token,
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
						checkResponseCode(res[1].data)
          }).catch(err => {
            resolve(err)
          })
        },
        fail: function(err) {
          uni.request({
            method: 'POST',
            url: baseUrl + url,
            data: data,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
            checkResponseCode(res[1].data)
          }).catch(err => {
            resolve(err)
          })
        }
      });
    })
  },
  deletes(url, param) {
    return new Promise((resolve, reject) => {
      uni.getStorage({
        key: 'isLogin',
        success:ress=> {
          let token = ress.data
          uni.request({
            method: 'DELETE',
            url: baseUrl + url,
            header: {
              'Authorization': token,
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
          }).catch(err => {
            resolve(err)
          })
        },
        fail: function(err) {
          uni.request({
            method: 'POST',
            url: baseUrl + url,
            data: param,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            }
          }).then(res => {
            resolve(res[1].data)
            checkResponseCode(res[1].data)
          }).catch(err => {
            resolve(err)
          })
        }
      });
    })
  },
  request(method, url, data) {
    if (!method || !url) {
      console.log('Method or Url is undefind!')
      return
    }
    method = method.toLowerCase()
    if (method === 'get') {
      return this.get(url, data)
    }
    if (method === 'post') {
      return this.post(url, data)
    }
    if (method === 'delete') {
      return this.delete(url, data)
    }
    if (method === 'put') {
      return this.put(url, data)
    }
  },
  req(path, data) {
    if (!path) {
      console.log('Path is undefind!')
      return
    }
    // TODO 对象或是字符串
    let method = path.method
    let uri = path.uri
    return this.request(method, uri, data)
  },
  async syncReq(url, param) {
    let result = await uni.request({
      method: 'get',
      url,
      params: param,
      cancelToken: new CancelToken(c => {
        cancel = c
      })
    })
    return result
  }
}

function checkResponseCode(data) {
	let code = data.code
	// console.log(code)
	if (code === '-1') {
		// 登录过期
		uni.clearStorage()
		uni.reLaunch({
			 url: '/pages/login/login'
		})
		return false
	} else {
		let message = response2Message(data)
		if (message) {
			alterMessage(message)
		}
	}
}

function response2Message(resp) {
  let message = null
  let code = resp.code
  if (code !== '10000') {
    switch (code) {
      case '200':
        break
      case '2000':
        message = '请求参数错误，请检查参数'
        break
      case '20001':
        message = '任务不存在'
        break
      case '20002':
        message = '没有可接取的任务'
        break
      case '20003':
        message = '有未提交的任务'
        break
      case '20004':
        message = '提交过期,请重新接单'
        break
      case '20005':
        message = '任务已关闭'
        break
			case '20006':
				message = '正在审核,请勿重复提交'
				break
			case '20007':
				message = '没有可接取的任务'
				break
      default:
        message = resp.msg
        break
    }
    return message
  }
}

function alterMessage(msg) {
  if (msg !== null) {
    uni.showModal({
      title: '提示',
      content: msg
    });
  }
}
